/**
 * 工具类
 */
/*Use AES to encrypt a string*/
import CryptoJS from 'crypto-js/crypto-js'
export default {
    //获取session
	getSession() {
		let _str = '';
		const href = window.location.href;
        const session = _str = href.substring(href.length-32);
        const locationSession = window.localStorage.getItem('NewCCsession');
        if(locationSession && session == locationSession) {
			_str = window.localStorage.getItem('NewCCsession');
        } else {
			_str = session;
        }
        window.localStorage.setItem('NewCCsession',session);
        return _str;
	},
	//加密
	_getAesString(data, key, iv) {
		var sendData = CryptoJS.enc.Utf8.parse(data);
		key = CryptoJS.enc.Utf8.parse(key);
		iv = CryptoJS.enc.Utf8.parse(iv);
		var encrypted = CryptoJS.AES.encrypt(sendData, key,
			{
				iv: iv,
				mode: CryptoJS.mode.CBC,
				padding: CryptoJS.pad.Iso10126
			});
		return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
	},

	/*Use AES to decrypt a string*/
	//解密
	_getDAesString(encrypted, key, iv) {
		var key = CryptoJS.enc.Utf8.parse(key);
		var iv = CryptoJS.enc.Utf8.parse(iv);
		var decrypted = CryptoJS.AES.decrypt(encrypted, key,
			{
				iv: iv,
				mode: CryptoJS.mode.CBC,
				padding: CryptoJS.pad.Iso10126
			});
		return decrypted.toString(CryptoJS.enc.Utf8);
	},
	// 去除空格
	_spaceCheck(str) {
		if(str.replace(/\s+/g,"") == "") {
			return false;
		}else{
			return true;
		}
	},
	// 验证手机号
	_phoneCheck(str) {
		let reg = /^1[3456789]\d{9}$/;
		if(reg.test(str)){
			return true;
		}else{
			return false;
		}
	},
	// 验证1-32位数字
	_check1_32Num(str) {
		let reg = /^.{1,32}$/;
		if(reg.test(str)){
			return true;
		}else{
			return false;
		}
	},
	// 验证6-32位数字
	_check6_32Num(str) {
		let reg = /^\d{6,32}$/;
		if(reg.test(str)){
			return true;
		}else{
			return false;
		}
	},
	//限制账号名
	_regularNickname(val) {
		var reg = /[^0-9a-zA-Z_\-\u4e00-\u9fa5]+/;
		var newVal = val.replace(reg, "");
		return newVal;
	},
	// 关闭父级弹框
	_closeParent(id) {
		parent.parent.document.getElementById(id).remove();
	},
	_setV(target, dataType ,val){
		target.attr(dataType, val);
	},
	_getV(target, dataType) {
		return target.attr(dataType);
	},
	_setPV(id, dataType ,val){
		let idx = "#"+id;
		$(idx, window.parent.document).attr(dataType, val);
	},
	_getPV(id, dataType) {
		let idx = "#"+id;
		return $(idx, window.parent.document).attr(dataType);
	},
	// 切换radio
	_changeRadio(_this, type) {
		let index = _this.index();
		let selfSelect = _this.parent(".selfSelect");
		let inputBox_obj = $(".show_input_box");
		_this.addClass("selected").siblings("li").removeClass("selected");
		selfSelect.attr("data-value", _this.find('span').attr("data-value"));
		if(type == "toggleShow"){
			if(inputBox_obj.eq(index).hasClass("hide")){
				inputBox_obj.eq(index).removeClass("hide").siblings(".show_input_box").addClass("hide");
			};
		}
	},
	// 切换Input框查看状态
	_changeInput(_this, obj) {
		if(_this.hasClass("i-eyeView-show")) {
			_this.removeClass("i-eyeView-show").addClass("i-eyeView-hide");
			obj.attr("type","password");
		}else {
			_this.removeClass("i-eyeView-hide").addClass("i-eyeView-show");
			obj.attr("type","text");
		}
	},
	fnNiceScroll(targetObj ,_color, _cursorwidth, _cursorborderradius) {
        // var objArr = this;
        // console.log(objArr);
        var objArr = targetObj;
		$.each(objArr, function(idx,obj) {
			obj = $(obj);
			_color = _color || "#e9e9e9";
			_cursorwidth = _cursorwidth || "5px";
			_cursorborderradius = _cursorborderradius || "5px";

			$.fn.niceScroll && obj.niceScroll({
				cursorcolor: _color,
				cursoropacitymax: 1,
				touchbehavior: false,
				cursorwidth: _cursorwidth,
				cursorborder: "0",
				cursorborderradius: _cursorborderradius
			});
		});
	},
	// _confirmTips(text, confirmFun, cancelFun, _defineBtnValue, _cancelBtnValue) {
	// 	var defineBtnValue = typeof _defineBtnValue == "undefined" ? "确认" : _defineBtnValue;
    //     var cancelBtnValue = typeof _cancelBtnValue == "undefined" ? "取消" : _cancelBtnValue;
	// 	var $body = top.$("body", top.document);
	// 	var _html = '<div class="simulate_popupFilterBg ' + typeFlag + '_popupBg"></div>\
	// 		<div class="simulate_popupContent ' + typeFlag + '_popupContent">\
	// 		<div class="simulate_popupTipsText ' + typeFlag + '_tipsText"></div>' +
	// 		(typeFlag == "prompt" ? '<input type="text" class="prompt_inputTexts"/>' : '') + operateStr +
	// 		'</div>';
	// 	$body.prepend(_html);
	// }
}